﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace GPSTrackingClientsWebsiteManager.Espanol.Pages
{
    public partial class ImeiGroup : Telerik.Web.UI.RadAjaxPage
    {
        private void GetImeiByUsername(string Username)
        {                        
            ImeiGrid.DataSource = GPSTrackingServerDBCustom.Data.DataRepository.ImeiProvider.GetDataTableImeiByUsername(Username);
            ImeiGrid.Columns[4].Visible = false;
        }


        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ImeiGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            GetImeiByUsername(Request.QueryString["Username"]);
        }


        protected void ImeiGrid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            try
            {
                if (e.Item is GridDataItem)
                {
                    GridDataItem gridDataItem = e.Item as GridDataItem;
                    RadComboBox RadComboBox1 = gridDataItem.FindControl("RadComboBox1") as RadComboBox;

                    GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.UsersByGroups> usersByGroupsEntities = GPSTrackingServerDB.Data.DataRepository.UsersByGroupsProvider.GetByUsername(Request.QueryString["Username"]);

                    GPSTrackingServerDB.Data.DataRepository.UsersByGroupsProvider.DeepLoad(usersByGroupsEntities, true, GPSTrackingServerDB.Data.DeepLoadType.IncludeChildren, typeof(GPSTrackingServerDB.Entities.Groups));

                    //Parse
                    GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.Groups> groupsEntities = new GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.Groups>();
                    foreach (GPSTrackingServerDB.Entities.UsersByGroups entity in usersByGroupsEntities)
                    {
                        groupsEntities.Add(entity.GroupIdSource);
                    }

                    RadComboBox1.DataSource = groupsEntities;
                    RadComboBox1.DataTextField = "GroupName";
                    RadComboBox1.DataValueField = "GroupId";
                    RadComboBox1.DataBind();


                    RadComboBox1.SelectedValue = e.Item.Cells[6].Text;
                }
            }
            catch (Exception ex)
            {

                GPSTrackingLogCustom.WebsiteLog.Error(ex.Message);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_miniError('" + ex.Message + "','Grupos error ...');", true);
            }  
        }

        protected void ImeiGrid_ItemCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                foreach (GridDataItem item in ImeiGrid.Items)
                {
                    RadComboBox RadComboBox1 = item.FindControl("RadComboBox1") as RadComboBox;

                    if (!RadComboBox1.SelectedValue.Equals(item.Cells[6].Text))
                    {
                        GPSTrackingServerDB.Entities.GroupsByImei groupsByImeiEntity = new GPSTrackingServerDB.Entities.GroupsByImei();
                        groupsByImeiEntity.Imei = item.Cells[2].Text;
                        groupsByImeiEntity.GroupId = Convert.ToInt32(RadComboBox1.SelectedValue);
                        groupsByImeiEntity.OriginalGroupId = Convert.ToInt32(item.Cells[6].Text);
                        groupsByImeiEntity.OriginalImei = item.Cells[2].Text;

                        GPSTrackingServerDB.Data.DataRepository.GroupsByImeiProvider.Update(groupsByImeiEntity);
                    }
                }

                GetImeiByUsername(Request.QueryString["Username"]);
                ImeiGrid.DataBind();

                RadNotification1.Show();
            }
            catch (Exception ex)
            {

                GPSTrackingLogCustom.WebsiteLog.Error(ex.Message);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_miniError('" + ex.Message + "','GPS error ...');", true);
            } 
        }

    }
}